import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import tree from '@dfeidao/fd-w000001';

export default async function a007(fd: IFeidaoAiBrowserComponent) {
	const name = get(fd, 'name');
	const client_type = get(fd, 'client_type');
	const tenantid = get(fd, 'tenantid');
	// console.log(client_type, '00');
	// const tenant_level = get(fd, 'tenant_level');
	if (!name) {
		await error('请先选择租户！');
		return;
	}
	const zds = fd.data.node.querySelector('[zds]') as tree;

	const checked = zds.get_checked();
	const att = new Array();
	checked.forEach((i) => {
		if (i.no && att.indexOf(i.no) === -1) {
			att.push(i.no);
		}
	});
	const arr = att.map((y) => {
		return zds.get_data_by_id(y);
	});
	arr.forEach((i) => {
		checked.push(i);
	});
	const rey = new Array();
	checked.forEach((item) => {
		rey.push(item.no);
	});
	// checked.forEach((i))
	zds.check_node(rey);
	const resultarr = new Array();
	for (let i = 0; i < checked.length; i++) {
		if (resultarr.indexOf(checked[i]) === -1) {
			resultarr.push(checked[i]);
		}
	}
	const fujd = new Array();
	const zjd = new Array();
	resultarr.forEach((i) => {
		if (i.no) {
			const arr1 = { action_keyword: i.action_keyword, action_name: i.action_name, code: i.code, formid: i.no, productid: tenantid };
			zjd.push(arr1);
		} else {
			fujd.push({ formid: i.formid, formname: i.formname, package_no: i.package_no, package_name: i.package_name, package_path: i.package_path, create_time: i.create_time, client_type, productid: tenantid, source_flag: '1' });
		}
	});
	const param = {
		filter: {
			tenantid,
			fujd,
			zjd,
			client_type
		}
	};
	// return;
	const res1 = await nodejs('tenant-function/zj-000001/s003',
		param
	);
	if (res1 === 'success') {
		success('保存成功');
	} else {
		error('保存失败');
	}
}
